// 密码维护
<template>
  <el-form autocomplete="off" ref="adminModify" label-position="right" :rules="adminRules" label-width="105px"
    size="medium" :model="adminModify">
    <el-form-item label="新密码" prop='new_password' class="tip">
      <el-input type="password" v-model.trim="adminModify.new_password" placeholder="请输入新密码"></el-input>
    </el-form-item>
    <el-form-item label="重复新密码" prop='new_password_confirmation' class="tip">
      <el-input type="password" size="medium" v-model.trim="adminModify.new_password_confirmation"
        placeholder="请再次输入新密码"></el-input>
    </el-form-item>
    <el-form-item>
      <div class="flex-justify-bcenter">
        <el-button @click="resetForm('adminModify')">清空</el-button>
        <el-button type="primary" :loading='adminModify_btn' @click="submitForm('adminModify')">确定
        </el-button>
      </div>
    </el-form-item>
  </el-form>
</template>

<script>
  import {
    ApipasswordReset
  } from '@/api'
  export default {
    props:['code','editPassword'],
    data() {
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.adminModify.new_password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        adminModify_btn: false,
        adminRules: {
          new_password: [{
            required: true,
            message: '请输入新密码',
            trigger: 'blur'
          }],
          new_password_confirmation: [{
            validator: validatePass2,
            trigger: 'blur',
            required: true,
          }],
        },
        //管理员重置密码
        adminModify: {
          employee_code: this.code,
          new_password: "",
          new_password_confirmation: ''
        }
      }
    },
    methods: {
      submitForm(ele) {
        console.log(this[ele])
        this.$refs[ele].validate((valid) => {
          if (valid) {
            const loadingBtn = ele + '_btn'
            this[loadingBtn] = true
            ApipasswordReset(this[ele]).then(res => {
              this[loadingBtn] = false
              this.$message.success('修改成功！该职员新密码为：' + this[ele].new_password)
              this.resetForm(ele)
              this.$emit('update:editPassword', false)
            }).catch(() => {
              this.$message.error('修改失败！')
              this[loadingBtn] = false
            })
          } else {
            return false;
          }
        });
      },
      resetForm(ele) {
        this.$refs[ele].resetFields();
      }
    }
  }
</script>

<style lang='less' scoped>
/deep/.el-form-item__label:before{
  content: '' !important;
}
</style>